﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Collections;
using System.Transactions;

#region Comentario
namespace Distrackting.DataLayer
{
    public partial class Comentario
    {
        public void setComentario(Comentario c)
        {
            this.cod_comentario = c.cod_comentario;
            this.cod_utilizador = c.cod_utilizador;
            this.cod_percurso = c.cod_percurso;
            this.data = c.data;
            this.texto = c.texto;
        }
    }
}
#endregion


#region dataComentario
namespace Distrackting.DataLayer
{
    public class dataComentario
    {
        #region Methods
        public static void createComentario(Comentario c)
        {

            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                c.cod_comentario = System.Guid.NewGuid();
                dbData.Comentarios.InsertOnSubmit(c);
                dbData.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        

        public static Comentario getComentario(Guid guid)
        {
            try
            {
                if (guid == null) return null;
                dbDataDataContext dbData = new dbDataDataContext();

                Comentario cmt = (from c in dbData.Comentarios
                             where c.cod_comentario == guid
                             select c).FirstOrDefault();
                return cmt;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean changeComentario(Comentario c)
        {
            try
            {
                if (c == null)
                    return false;
                else
                {
                    dbDataDataContext dbData = new dbDataDataContext();
                    Comentario res = dbData.Comentarios.FirstOrDefault(cmt => cmt.cod_comentario == c.cod_comentario);
                    if (res == null) return false;
                    else
                    {
                        res.setComentario(c);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Comentario> getComentarios()
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Comentarios;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Comentario> getComentarios(Expression<Func<Comentario, bool>> whereClause)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Comentarios.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean deleteComentario(Comentario c)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                if (dbData.Comentarios.Contains(c))
                {
                    Comentario res = (from cmt in dbData.Comentarios
                                 where cmt.cod_comentario == c.cod_comentario
                                 select cmt).FirstOrDefault();
                    if (res == null)
                        return false;
                    else
                    {
                        dbData.Comentarios.DeleteOnSubmit(c);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
                else return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }


        #endregion
    }
}
#endregion